package cn.stylefeng.guns.modular.gridsystem.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.modular.gridsystem.entity.BizUploadQueueLog;
import cn.stylefeng.guns.modular.gridsystem.mapper.BizUploadQueueLogMapper;
import cn.stylefeng.guns.modular.gridsystem.model.param.BizUploadQueueLogParam;
import cn.stylefeng.guns.modular.gridsystem.service.BizUploadQueueLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * 网关上报分数表 服务实现类
 *
 * @author ssy
 * @date 2021/11/02 18:06
 */
@Service
public class BizUploadQueueLogServiceImpl extends ServiceImpl<BizUploadQueueLogMapper, BizUploadQueueLog> implements BizUploadQueueLogService {


    /**
     * 添加网关上报分数表
     *
     * @param bizUploadQueueLogParam
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(BizUploadQueueLogParam bizUploadQueueLogParam) {
        // 将dto转为实体
        BizUploadQueueLog bizUploadQueueLog = new BizUploadQueueLog();
        BeanUtil.copyProperties(bizUploadQueueLogParam, bizUploadQueueLog);
        this.save(bizUploadQueueLog);
    }

    /**
     * 删除网关上报分数表
     *
     * @param bizUploadQueueLogParam
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(BizUploadQueueLogParam bizUploadQueueLogParam) {
        this.removeById(bizUploadQueueLogParam.getId());
    }

    /**
     * 编辑网关上报分数表
     *
     * @param bizUploadQueueLogParam
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void edit(BizUploadQueueLogParam bizUploadQueueLogParam) {
        // 根据id查询实体
        BizUploadQueueLog bizUploadQueueLog = this.queryBizUploadQueueLog(bizUploadQueueLogParam);
        // 请求参数转化为实体
        BeanUtil.copyProperties(bizUploadQueueLogParam, bizUploadQueueLog);
        this.updateById(bizUploadQueueLog);
    }

    /**
     * 查看详情网关上报分数表
     *
     * @param bizUploadQueueLogParam
     * @return
     */
    @Override
    public BizUploadQueueLog detail(BizUploadQueueLogParam bizUploadQueueLogParam) {
        return this.queryBizUploadQueueLog(bizUploadQueueLogParam);
    }

    /**
     * 新增上报分数表
     *
     * @param bizUploadQueueLogParam
     */
    @Override
    public void addAndReturnId(BizUploadQueueLogParam bizUploadQueueLogParam) {
        this.baseMapper.addAndReturnId(bizUploadQueueLogParam);
    }

    /**
     * 网关上报分数表_分页查询
     *
     * @param uploadQueueLogParam
     * @return
     */
    @Override
    public PageResult<Map<String, Object>> pageList(BizUploadQueueLogParam uploadQueueLogParam) {
        String[] list = new String[1];
        String gatewayIds = uploadQueueLogParam.getGatewayIds();
        if (StringUtils.isNotBlank(gatewayIds)) {
            list = gatewayIds.split(",");
        } else {
            list[0] = "";
        }
        return new PageResult<>(this.baseMapper.listMap(PageFactory.defaultPage(), uploadQueueLogParam, list));
    }


    /**
     * 获取网关上报分数表
     *
     * @author ssy
     * @since 2021/11/02 18:06
     */
    private BizUploadQueueLog queryBizUploadQueueLog(BizUploadQueueLogParam bizUploadQueueLogParam) {
        return this.getById(bizUploadQueueLogParam.getId());
    }
}
